/*******************************************************************************
 * Copyright (c) 2005 Ultimania Organization. All rights reserved. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/org/documents/epl-v10.html
 * 
 * Contributors: tidus - initial implementation.
 ******************************************************************************/

package org.ultimania.middlegenide.core;

import java.util.Properties;


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class EJBSubPlugin extends SubPlugin {
	private Text jndiprefix = null;
	private Text datasource = null;
	private Combo ejbver = null;

	
	private static final String BUILD_TEMPLATE = "build-ejb.xml.vm";

	private static final String JNDIPREFIX = "jndiprefix";
	private static final String DATASOURCE = "datasource_jndi_name";

	private static final String EJBVER = "ejbver";
	private static final String[] EJB_VERS = new String[] { "ejb2.x", "ejb3" };


	public EJBSubPlugin(Properties property) {
		// set jar repositry.
		this.property = property;
		String c = property.getProperty(JNDIPREFIX);
		if (c == null)
			property.setProperty(JNDIPREFIX, "ejb");
	}

	public void generate() throws GeneratorException {
		try {
			// generate build file.
			mergeTemplate(BUILD_TEMPLATE, buildFile);

			// run ant. Then mapping files and JavaBeans are generated.
			runAnt();

			// generate hibernate configuration file.
		} catch (Exception e) {
			throw new GeneratorException(e);
		}
	}

	public void configure(Properties p) {
		EJBDialog dialog = new EJBDialog(Display.getCurrent().getActiveShell());
		dialog.setProperties(p);
		dialog.create();
		dialog.open();
		dialog.close();
	}

	public static void main(String args[]) {
		EJBSubPlugin p = new EJBSubPlugin(new Properties());
		p.configure();
	}

	class EJBDialog extends Dialog {
		Properties p;

		void setProperties(Properties p) {
			this.p = p;
		}

		protected Control createDialogArea(Composite container) {
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			layout.verticalSpacing = 9;
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			container.setLayout(layout);
			
			//------------
			Label l = new Label(container,SWT.NULL);
			l.setText("JNDI Prefix:");

			jndiprefix = new Text(container,SWT.BORDER|SWT.SINGLE);
			String c = p.getProperty(JNDIPREFIX);
			jndiprefix.setText(c);
			jndiprefix.setLayoutData(gd);

			//------------
			l = new Label(container,SWT.NULL);
			l.setText("Datasource:");
			datasource = new Text(container,SWT.BORDER|SWT.SINGLE);
			c = p.getProperty(DATASOURCE);
			if(c==null){
				c = "java:/DefaultDS";
			}
			datasource.setText(c);
			gd = new GridData(GridData.FILL_HORIZONTAL);
			datasource.setLayoutData(gd);
			
			l = new Label(container,SWT.NULL);
			l.setText("EJB Version:");
			ejbver = new Combo(container, SWT.BORDER|SWT.SINGLE);
			ejbver.setItems(EJB_VERS);
			c = p.getProperty(EJBVER);
			if(c!=null){
				if(c.equals(EJB_VERS[0])){
					ejbver.select(0);
				} else {
					ejbver.select(1);
				}
			} else {
				ejbver.select(0);
			}
			gd = new GridData(GridData.FILL_HORIZONTAL);
			ejbver.setLayoutData(gd);
			
			return super.createDialogArea(container);
		}

		public EJBDialog(Shell parent) {
			super(parent);
		}

		protected void configureShell(Shell newShell) {
			newShell.setText("EJB Configuration");
			super.configureShell(newShell);
		}
		
		protected void buttonPressed(int buttonId) {
			if(buttonId==Dialog.OK){
				p.setProperty(JNDIPREFIX, (String) jndiprefix.getText());
				p.setProperty(EJBVER, (String)ejbver.getItem(ejbver.getSelectionIndex()));
				p.setProperty(DATASOURCE, (String) datasource.getText());
			}
			super.buttonPressed(buttonId);
		}
		
		protected void constrainShellSize() {
			Shell shell = getShell();
			shell.pack();
		}
		
	}
	
	
}
